package com.casstime.ec.cloud.meeting.service.handler;

import com.casstime.commons.service.exception.HttpMessageException;
import com.casstime.ec.cloud.meeting.dao.entity.EntityDepartment;
import com.casstime.ec.cloud.meeting.dao.entity.EntityUser;
import com.casstime.ec.cloud.meeting.dao.entity.example.EntityUserExample;
import com.casstime.ec.cloud.meeting.dao.mapper.EntityUserMapper;
import com.casstime.ec.cloud.meeting.model.Department;
import com.casstime.ec.cloud.meeting.values.UserDTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserHandler {

   @Autowired
   private EntityUserMapper entityUserMapper;


  /**
   * 登录的方法
   * @param user
   * @return
   */
   public UserDTO login(UserDTO user) {
       UserDTO userDTO = new UserDTO();
       userDTO.setAvatar(user.getAvatar());
       userDTO.setRoleId(user.getRoleId());
       userDTO.setUsername(user.getUsername());
       userDTO.setPassword(user.getPassword());

       EntityUserExample entityUserExample = new EntityUserExample();
       entityUserExample.createCriteria()
           .andUsernameEqualTo(user.getUsername())
           .andPasswordEqualTo(user.getPassword())
           .andRoleIdEqualTo(user.getRoleId());
     List<EntityUser> list =  entityUserMapper.selectByExample(entityUserExample);
     if(list.size() == 0) {
        return null;
     } else {
       userDTO.setId(list.get(0).getId());
       return userDTO;
     }

   }



  public Department getDepartmentByUserId(Integer userId) {
    if(userId == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    EntityDepartment entityDepartment = entityUserMapper.getDepartmentByUserId(userId);
    if(entityDepartment == null) {
      return null;
    }
    return Department.get(entityDepartment);
  }



}
